package excute;

import org.htmlcleaner.XPatherException;
import schedual.PnSchedual;
import schedual.Schedual;
import schedual.UrlSchedual;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main {

    public static void main(String[]args) throws Exception {
        Spilder spilder = new Spilder();
        Schedual urlSchedual =new UrlSchedual();
        Schedual pnSchedual =new PnSchedual();
        Runnable saveUrls = new Runnable() {
            @Override
            public void run() {
                try {
                    String pn = pnSchedual.poll("pn");
                    if(pn==""||pn==null)
                         spilder.spildUrl("https://tieba.baidu.com/f?kw=黑丝&ie=utf-8&pn=",0+"");
                    else
                        spilder.spildUrl("https://tieba.baidu.com/f?kw=黑丝&ie=utf-8&pn=",pn);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (XPatherException e) {
                    e.printStackTrace();
                }
            }
        };

        Runnable saveDetail = new Runnable() {
            @Override
            public void run() {
                while(true){
                    try {
                        String url = urlSchedual.poll("urls");
                        if(url!=null)
                            spilder.spildContent(url);
                        else
                            System.out.println("没有链接了");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        ExecutorService threadPool = Executors.newCachedThreadPool();
        threadPool.submit(saveUrls);
        threadPool.submit(saveDetail);
        threadPool.shutdown();
    }


}
